package org.zn.note.jdk.thread.util.concurrent;

import org.zn.note.common.util.ThreadUtil;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName TaskCallable
 * @Author zn
 * @Date 2019/4/2 22:17
 **/
public class TaskCallable implements Callable<String> {
    private long sleepTime;
    private TimeUnit timeUnit;

    public TaskCallable(long sleepTime) {
        this.sleepTime = sleepTime;
        this.timeUnit = TimeUnit.SECONDS;
    }

    public TaskCallable(TimeUnit timeUnit, long sleepTime) {
        this.sleepTime = sleepTime;
        this.timeUnit = timeUnit;
    }

    @Override
    public String call() throws Exception {
        long startTime = System.currentTimeMillis();
        ThreadUtil.printThread("start ...");
        ThreadUtil.sleep(timeUnit, sleepTime); // 任务执行了5秒
        ThreadUtil.printThread("return . spend time [" + (System.currentTimeMillis() - startTime) + "]");
        return Thread.currentThread().getName(); // 返回处理结果
    }
}
